package cn.mw.cmdb.param;

import cn.mw.cmdb.entity.ModelGroupInfo;
import cn.mw.cmdb.entity.MwCMDBConfigInfo;
import cn.mw.cmdb.service.ServiceChoose;
import cn.mw.cmdb.service.impl.CmdbServiceManage;
import cn.mw.cmdb.service.impl.ModelGroupServiceImpl;
import cn.mw.cmdb.service.impl.MwCMDBConfigImpl;
import cn.mw.cmdb.util.CMDBTool;
import cn.mw.components.mongodb.entity.CriteriaOpsType;
import cn.mw.components.mongodb.entity.SearchParam;
import cn.mw.components.mongodb.entity.SubCondition;
import cn.mw.microMonitorCommon.util.SpringUtils;
import lombok.Data;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

import static cn.mw.cmdb.enums.ModelConfigKeyEnum.systemDevType;
import static cn.mw.cmdb.param.SystemDevTypeEnum.scBankSystemDev;

@Data
public class GroupSearchParam extends SearchParam implements ServiceChoose, SearchParamConvert {

    private String groupName;


    private List<String> groupNameList;

    @Override
    public void convert() throws Exception {
        //如果是四川银行，模型分组，模型查询时，不做权限过滤
        CmdbServiceManage cmdbServiceManage = SpringUtils.getBean(CmdbServiceManage.class);
        MwCMDBConfigImpl configServer = (MwCMDBConfigImpl) cmdbServiceManage.getModelService(MwCMDBConfigImpl.ID);
        MwCMDBConfigInfo cmdbConfigInfo = configServer.doSelectByKey(systemDevType.name());
        Object configValue = cmdbConfigInfo.getConfigValue();
        if (configValue instanceof String) {
            String systemType = (String) configValue;
            if (scBankSystemDev.name().equals(systemType)) {
                this.setIgnoreDataAuth(true);
            }
        }
        //资产类型树结构查询参数
        if (StringUtils.hasText(groupName)) {
            this.addSubCondition(CMDBTool.getFieldName(ModelGroupInfo::getName)
                    , new SubCondition(CriteriaOpsType.is.name(), groupName));
        }

        //资产类型树结构查询参数
        if (CollectionUtils.isNotEmpty(groupNameList)) {
            this.addSubCondition(CMDBTool.getFieldName(ModelGroupInfo::getName)
                    , new SubCondition(CriteriaOpsType.in.name(), groupNameList));
        }
    }

    @Override
    public String getServiceId() {
        return ModelGroupServiceImpl.ID;
    }
}
